<template>
  <div style="height:600px; ">
      <div class="pagetop">
          <div style="width:100%">
              <table style="font-size:12px;width:100%" class="table1style">
                  <tr>
                      <td>报告单元</td>
                      <td>
                          <rm-ddl-rptunit prgno="LIS310" v-model="rptunit" filterable :clearable="false" @change="handlerrptitemchange($event)" defaultflag="1" :appendtobody="true" visible-change:true width="100%" @keyup.enter.native="inputautofocus($event, 'popinput1')" ref="popinput1"></rm-ddl-rptunit>
                      </td>
                      <td>标本日期</td>
                      <td>
                          <el-date-picker v-model="sampledatetime" type="date" placeholder="选择日期" style="width:100%" @change="sampledatetimechange()" value-format="yyyy-MM-dd">
                          </el-date-picker>
                      </td>
                      <td>样本号</td>
                      <td>
                          <el-input v-model="sampleno" placeholder="请输入样本号" style="width:100%" @keyup.enter.native="inputautofocus($event, 'popinput3')" ref="popinput3"></el-input>
                      </td>
                      <td></td>
                  </tr>
                  <tr>
                      <td>报告项目</td>
                      <td>
                          <el-input ref="txtitemname" v-model="rpt_itemname" placeholder="双击选择调整项目" @dblclick.native="OpenRptitem" clearable @clear="clearitem"></el-input>
                      </td>
                      <td>检验仪器</td>
                      <td>
                          <el-select placeholder="请选择仪器" style="width:100%" clearable v-model="instrument">
                              <el-option :key="index" :label="item.label" :value="item.value" v-for="(item, index) in InstrumentList"></el-option>
                          </el-select>
                      </td>
                      <td>审核状态</td>
                      <td>
                          <el-select placeholder="请选择审核状态" style="width:100%" clearable v-model="rechkflag">
                              <el-option label="全部" value=""></el-option>
                              <el-option label="已审核" value="9"></el-option>
                              <el-option label="未审核" value="0"></el-option>
                          </el-select>
                      </td>
                  </tr>
                  <tr v-if="ResultList&&ResultList.length>0">
                      <td>
                          <el-select style="width:80px;" v-show="ResultList.length>1" placeholder="请选择结果" v-model="resultnum" @change="resultnumChange" clearable>
                              <el-option :key="index" :label="item.label" :value="item.value" v-for="(item, index) in ResultList"></el-option>
                          </el-select>
                          <span v-show="ResultList.length==1">{{ResultList[0].label}}</span>

                      </td>
                      <td v-if="ResultList&&ResultList.length>0">
                          <el-select v-if="resulttxtList&&resulttxtList.length>0&&resulttxtList.length<20" placeholder="请选择结果" style="width:100%;" v-model="resulttxt" clearable>
                              <el-option :key="index" :label="item" :value="item" v-for="(item, index) in resulttxtList"></el-option>
                          </el-select>
                          <el-input v-else v-model="resulttxt" placeholder="输入结果内容" style="width:100%" ref="popinput4"></el-input>
                      </td>
                      <td>
                          <el-checkbox v-if="ResultList&&ResultList.length>0" v-model="checked">模糊</el-checkbox>
                      </td>
                  </tr>
                  <tr>
                      <td>
                          <el-button icon="el-icon-document" style="width:80px;" @click="changeredoflag()" type="primary">全部设为</el-button>
                      </td>
                      <td>
                          <el-select placeholder="请选择" style="width:100%" v-model="selectmodel" ref="lgusys">
                              <el-option label="重新计算" value="0"></el-option>
                              <el-option label="需复查" value="M"></el-option>
                              <el-option label="已复查" value="2"></el-option>
                              <el-option label="无需复查" value="N"></el-option>
                          </el-select>
                      </td>
                      <td>{{showmsg}}</td>
                      <td></td>
                      <td></td>
                      <td align="right">
                          <el-button icon="el-icon-search" @click="btnRead()">查询</el-button>
                          <el-button icon="el-icon-document" @click="save()">确定</el-button>
                          <el-button icon="el-icon-circle-close" @click="cancel()">取消</el-button>
                      </td>
                  </tr>
              </table>
          </div>
      </div>
      <div class="pagecenter">
          <div class="middle-center" :style="{'height':ResultList&&ResultList.length>0?'480px':'510px'}">
              <vxe-table :loading="loading" :data.sync="list" border fit :mouse-config="{ selected: true }" :edit-config="{ trigger: 'click', mode: 'cell', showStatus: true, showIcon: false }" height="100%" header-align="center" highlight-current-row highlight-hover-row ref="xTable" resizable show-header-overflow="tooltip" :row-class-name="ListClassName" show-overflow="tooltip" size="mini" :cell-class-name="cellClassName" @current-change="currentChangeEvent">
                  <vxe-table-column field="sampleno" title="样本号" width="60px"></vxe-table-column>
                  <vxe-table-column field="rpt_itemname" title="检验项目" width="170px"></vxe-table-column>
                  <vxe-table-column v-if="ResultList&&ResultList.length>0 && Result!=''" field="result1" :title="Result" width="60px">
                      <template v-slot="{ row }">
                          <span v-show="resultnum.toLowerCase()=='result1'">{{row.result1}}</span>
                          <span v-show="resultnum.toLowerCase()=='result2'">{{row.result2}}</span>
                          <span v-show="resultnum.toLowerCase()=='result3'">{{row.result3}}</span>
                          <span v-show="resultnum.toLowerCase()=='result4'">{{row.result4}}</span>
                          <span v-show="resultnum.toLowerCase()=='result6'">{{row.result6}}</span>
                      </template>
                  </vxe-table-column>
                  <vxe-table-column field="result_str" title="检验结果" width="60px">
                      <template v-slot="{ row }">
                          <div :style="{background: row.cp_resultbackgroundcolor,color: row.cp_resultcolor}" style="padding:0px 10px">{{ row.result_str }}</div>
                      </template>
                  </vxe-table-column>
                  <vxe-table-column field="redo_flag" title="复查标志" width="310px">
                      <template v-slot="{ row }">
                          <el-radio-group v-model="row.redo_flag" :style="{ background: RedoClass(row.redo_flag) }" @change="RedoflagChange($event,row)">
                              <el-radio label="0">重新计算</el-radio>
                              <el-radio label="M">需复查</el-radio>
                              <el-radio label="2">已复查</el-radio>
                              <el-radio label="N">无需复查</el-radio>
                          </el-radio-group>
                      </template>
                  </vxe-table-column>
                  <vxe-table-column field="result2" title="稀释系数" width="60px" :edit-render="{name: 'input', autoselect: true}"></vxe-table-column>
                  <vxe-table-column field="result_ref" title="参考值" width="60px"></vxe-table-column>
                  <vxe-table-column field="execmsg" title="执行结果" width="260px"></vxe-table-column>
              </vxe-table>
          </div>
      </div>

      <!--多选弹窗--->
      <el-dialog width="1000px" top="10px" v-el-drag-dialog :append-to-body="true" :close-on-click-modal="false" :visible.sync="outerVisible" :destroy-on-close="true" title="报告项目" v-if="diagx">
          <rm-chkboxSelect2 prgno="LIS310" @cancel="outerVisible = false;
        diagx = false;" :isAllRpt="1" :rptunitid="rptunit" :rptunitdisabled="true" @selitem="addrptitem" @selitems="addrptitems" ref="rptdialog"></rm-chkboxSelect2>
      </el-dialog>

  </div>
</template>

<script type="text/javascript">
import XEUtils from "xe-utils";
export default {
  name: "rmlabreportmultreview",
  props: ["txtrptunit", "sampledatetime"],
  data() {
      return {
          //ConfigVisible: false, //列配置显示标志
          rptunit: this.txtrptunit,
          rpt_itemname: "",
          instrument: "",
          selectmodel: "M",
          rpt_itemcode: "",
          justifyfun: "",
          sampleno: "",
          resultnum: "",
          resulttxt: "",
          rechkflag: "",
          userno: "LIS",
          checked: false,
          islogin: false,
          loading: false,
          diagx: false,
          outerVisible: false,
          tableData: [],
          selectrow: [],
          InstrumentList: [],
          ResultList: [],
          resulttxtList: [],
          resulttxtList1: [],
          resulttxtList2: [],
          resulttxtList3: [],
          resulttxtList4: [],
          resulttxtList5: [],
          resulttxtList6: [],
          Result: "",
          macaddress: "",
          showmsg: "",
          okcnt: 0,
          errcnt: 0
      };
  },
  watch: {

  },
  created() {
      if (this.rptunit) {
          this.getInstrumentList(this.rptunit);
          this.loadresulttxtList();
      }
  },
  mounted() { },
  updated() { },
  computed: {
      //模糊查询  名称|首拼
      list() {
          // let txt = this.resulttxt.trim();
          // if (txt) {
          //     let rest = this.gettableData(txt);
          //     return rest;
          // } else {
          return this.tableData;
          // }
      }
  },
  methods: {
      async loadresulttxtList() {
          if (typeof jsObj != "undefined") {
              var computeInfo = jsObj.GetComputeInfo();
              var arr = computeInfo.split("\r");
              if (arr.length == 3) {
                  this.macaddress = arr[0];
              }
          } else {
              this.macaddress = "Default";
          }
          await this.$tool
              .request({
                  url: "LIS/LIS3/LIS310",
                  method: "post",
                  data: {
                      action: "getmultReview",
                      rptunitid: this.rptunit,
                      sampledatetime: this.sampledatetime,
                      macaddress: this.macaddress
                  }
              })
              .then(res => {
                  this.resulttxtList1 = [];
                  this.resulttxtList2 = [];
                  this.resulttxtList3 = [];
                  this.resulttxtList4 = [];
                  this.resulttxtList5 = [];
                  this.resulttxtList6 = [];
                  res.Data.forEach(x => {
                      if (this.resulttxtList1.indexOf(x.result1) == -1 && x.result1 != '' && x.result1 != null)
                          this.resulttxtList1.push(x.result1);
                      if (this.resulttxtList2.indexOf(x.result2) == -1 && x.result2 != '' && x.result2 != null)
                          this.resulttxtList2.push(x.result2);
                      if (this.resulttxtList3.indexOf(x.result3) == -1 && x.result3 != '' && x.result3 != null)
                          this.resulttxtList3.push(x.result3);
                      if (this.resulttxtList4.indexOf(x.result4) == -1 && x.result4 != '' && x.result4 != null)
                          this.resulttxtList4.push(x.result4);
                      if (this.resulttxtList5.indexOf(x.result5) == -1 && x.result5 != '' && x.result5 != null)
                          this.resulttxtList5.push(x.result5);
                      if (this.resulttxtList6.indexOf(x.result6) == -1 && x.result6 != '' && x.result6 != null)
                          this.resulttxtList6.push(x.result6);
                  })
                  this.getresulttxtList(this.resultnum);
              })
              .catch(e => { });
      },
      resultnumChange(val) {
          this.Result = val ? this.ResultList.find(ele => ele.value == val).label : '';
          this.getresulttxtList(val);

      },
      gettableData(val) {
          switch (this.resultnum.toLowerCase()) {
              case "result1":
                  if (!this.checked)
                      return this.tableData.filter(x => x.result1 == val);
                  else
                      return this.tableData.filter(x => x.result1 != null && x.result1.indexOf(val) > -1);
              case "result2":
                  if (!this.checked)
                      return this.tableData.filter(x => x.result2 == val);
                  else
                      return this.tableData.filter(x => x.result2 != null && x.result2.indexOf(val) > -1);

              case "result3":
                  if (!this.checked)
                      return this.tableData.filter(x => x.result3 == val);
                  else
                      return this.tableData.filter(x => x.result3 != null && x.result3.indexOf(val) > -1);

              case "result4":
                  if (!this.checked)
                      return this.tableData.filter(x => x.result4 == val);
                  else
                      return this.tableData.filter(x => x.result4 != null && x.result4.indexOf(val) > -1);

              case "result6":
                  if (!this.checked)
                      return this.tableData.filter(x => x.result6 == val);
                  else
                      return this.tableData.filter(x => x.result6 != null && x.result6.indexOf(val) > -1);

          }
      },
      getresulttxtList(val) {
          if (val.toLowerCase() == "result1")
              this.resulttxtList = this.resulttxtList1;
          else if (val.toLowerCase() == "result2")
              this.resulttxtList = this.resulttxtList2;
          else if (val.toLowerCase() == "result3")
              this.resulttxtList = this.resulttxtList3;
          else if (val.toLowerCase() == "result4")
              this.resulttxtList = this.resulttxtList4;
          else if (val.toLowerCase() == "result5")
              this.resulttxtList = this.resulttxtList5;
          else if (val.toLowerCase() == "result6")
              this.resulttxtList = this.resulttxtList6;
          else
              this.resulttxtList = [];
      },
      RedoClass(redoflag) {
          var colorstr = "";
          switch (redoflag) {
              case "M":
                  colorstr = "yellow";
                  break;
              case "2":
                  colorstr = "rgb(00, 255, 00)";
                  break;
              case "N":
                  colorstr = "";
                  break;
          }
          return colorstr;
      },
      RedoflagChange(val, row) {
          if (!row.result2 && row.sflag == "1" && val == "M") {
              row.result2 = row.dilutionrate;
          }
      },
      handlerrptitemchange(rptunitid) {
          this.$nextTick(() => {
              this.rptunit = rptunitid;
              this.loadresulttxtList();
          });
          this.instrument = "";
          this.resulttxtList = [];
          this.resulttxt = "";
          this.getInstrumentList(rptunitid);

      },
      sampledatetimechange() {
          this.loadresulttxtList();
      },
      ListClassName({ row, rowIndex }) {
          if (row.rechkflag == "9") {
              return "RechkBack";
          }
      },
      getInstrumentList(rptunitid) {
          this.$tool
              .request({
                  url: "LIS/LIS3/LIS310",
                  method: "post",
                  data: {
                      action: "getInstrumentList",
                      rptunitid: rptunitid
                  }
              })
              .then(res => {
                  this.InstrumentList = res.data1;
                  this.ResultList = res.data2;
                  if (res.data2.length > 0) {
                      this.resultnum = res.data2[0].value;
                      this.Result = this.resultnum ? this.ResultList.find(ele => ele.value == this.resultnum).label : '';
                  }
                  else {
                      this.Result = '';
                      this.resultnum = '';
                  }
              })
              .catch(e => { });
      },
      OpenRptitem() {
          this.diagx = true;
          this.outerVisible = true;
      },
      addrptitem(c, n) {

          this.outerVisible = false;
          this.diagx = false;
          this.rpt_itemcode = c;
          this.rpt_itemname = n;
          this.InitPageDate();
      },
      clearitem() {
          this.rpt_itemcode = "";
          this.rpt_itemname = "";
          this.InitPageDate();
      },
      addrptitems(rows) {
          this.rpt_itemcode = "";
          this.rpt_itemname = "";
          if (rows.length > 0) {
              rows.forEach(x => {
                  this.rpt_itemcode = this.rpt_itemcode + x.rpt_itemcode + ',';
                  this.rpt_itemname = this.rpt_itemname + x.rpt_itemname + ',';
              })
          }
          this.rpt_itemname = this.rpt_itemname.substr(0, this.rpt_itemname.length - 1);
          this.outerVisible = false;
          this.diagx = false;
          this.InitPageDate();
      },
      changeredoflag() {
          this.tableData.forEach(x => {
              x.redo_flag = this.selectmodel;
          });
      },
      InitPageDate() {
          // if(!this.rpt_itemcode){
          //     this.$alert("请先选择项目！");
          //     this.$ref.txtitemname.focus();
          //     return;
          // }
          if (this.rpt_itemname == "")
              this.rpt_itemcode = "";
          this.showmsg = "";
          this.okcnt = 0;
          this.errcnt = 0;
          this.loading = true;
          this.$tool
              .request({
                  url: "LIS/LIS3/LIS310",
                  method: "post",
                  data: {
                      action: "getmultReview",
                      rptunitid: this.rptunit,
                      rpt_itemcode: this.rpt_itemcode,
                      sampledatetime: this.sampledatetime,
                      sampleno: this.sampleno,
                      resulttxt: this.resulttxt,
                      checked: this.checked,
                      resultnum: this.resultnum,
                      instrument: this.instrument,
                      rechkflag: this.rechkflag
                  }
              })
              .then(res => {
                  this.tableData = res.Data;
                  // this.resulttxtList1=[];
                  // this.resulttxtList2=[];
                  // this.resulttxtList3=[];
                  // this.resulttxtList4=[];
                  // this.resulttxtList6=[];
                  // this.tableData.forEach(x=>{
                  //     if(this.resulttxtList1.indexOf(x.result1) == -1 && x.result1!='' && x.result1!=null)
                  //         this.resulttxtList1.push(x.result1);
                  //     if(this.resulttxtList2.indexOf(x.result2) == -1 && x.result2!='' && x.result2!=null)
                  //         this.resulttxtList2.push(x.result2);
                  //     if(this.resulttxtList3.indexOf(x.result3) == -1 && x.result3!='' && x.result3!=null)
                  //         this.resulttxtList3.push(x.result3);
                  //     if(this.resulttxtList4.indexOf(x.result4) == -1 && x.result4!='' && x.result4!=null)
                  //         this.resulttxtList4.push(x.result4);
                  //     if(this.resulttxtList6.indexOf(x.result6) == -1 && x.result6!='' && x.result6!=null)
                  //         this.resulttxtList6.push(x.result6);
                  // })
                  // this.getresulttxtList(this.resultnum);
                  this.loading = false;
                  if (this.tableData && this.tableData.length > 0) {
                      this.showmsg = "共" + this.tableData.length + "条数据";
                  }
              })
              .catch(e => {
                  this.loading = false;
              });
      },
      cancel() {
          this.$emit("close", false);
      },
      save() {
          //要逐行提交，返回的消息放在execmsg中，要不几百条数据的话，页面就卡死了
          if (!this.tableData || this.tableData.length == 0) {
              this.$alert("请先查询出数据再标记！");
              return;
          }
          this.okcnt = 0;
          this.errcnt = 0;
          this.execsave(0);
      },
      async execsave(index) {
          if (index >= this.tableData.length) {
              this.showmsg = "共标记" + this.tableData.length + "条数据，成功" + this.okcnt + "条 "
              if (this.errcnt > 0) {
                  this.showmsg += ",失败" + this.errcnt + "条 ";
              }
              this.$message.success("批量标记成功");//共标记多少条，成功多少条，失败多少条，要显示在页面上，不能只弹一个消息
              return;
          } else {
              this.showmsg = "正在执行" + index + " / " + this.tableData.length + "";
              //查找相同reportid的数据，一个reportid提交一次后台
              let param = [];
              param.push(this.tableData[index]);
              let loopreportid = this.tableData[index].reportid;
              for (let i = index + 1; i < this.tableData.length; i++) {
                  if (this.tableData[i].reportid == loopreportid) {
                      param.push(this.tableData[i]);
                  }
              }
              let sindex = index;//本次提交数据的起始行号
              let eindex = index + (param.length - 1);//本次提交数据的结束行号
              try {
                  let res = await this.$tool.request({
                      url: "LIS/LIS3/LIS310",
                      method: "post",
                      data: {
                          action: "saveredoflag",
                          reportid: loopreportid,
                          tablestr: param
                      }
                  });
                  if (res.flag == 1) {
                      this.setTableDataExecmsg(sindex, eindex, 1, "标记成功");
                      this.okcnt += param.length;
                  } else {
                      this.setTableDataExecmsg(sindex, eindex, 0, res.msg);//要变红
                      this.errcnt += param.length;
                  }
              }
              catch (e) {
                  this.setTableDataExecmsg(sindex, eindex, 0, "发生异常:" + e);//要变红
                  this.errcnt += param.length;
              };
              // console.log('this.$refs.xTable.tableFullData',this.$refs.xTable.tableFullData);
              this.$nextTick(() => {
                  this.execsave(eindex + 1);
              });
          }
      },
      setTableDataExecmsg(sindex, eindex, okflag, msg) {

          console.log('msg', sindex, eindex, okflag, msg);
          for (let i = sindex; i <= eindex && i < this.tableData.length; i++) {
              this.tableData[i].execmsg = msg;
              //okflag!=1时要变红
          }
          // for(let i=sindex; i<=eindex && i<this.$refs.xTable.tableFullData.length; i++){
          //     console.log('msg',sindex ,eindex,okflag,msg);
          //     this.$refs.xTable.tableFullData[i].execmsg=msg;
          //     //okflag!=1时要变红
          // }
      },
      btnRead() {
          this.InitPageDate();
      },
      currentChangeEvent({ row }) { },
      cellClassName() { },
      inputautofocus(evt, element) {
          if (element == "popinput1") {
              this.$refs.popinput1.blur();
              this.$refs.popinput2.$refs.ddlrptitemSelect.focus();
          } else if (element == "popinput2") {
              this.$refs.popinput3.focus();
          } else if (element == "popinput3") {
              this.InitPageDate();
              this.$refs.popinput4.focus();
          } else {
              this.btnJustify();
          }
      }
  }
};
</script>

<style scoped>
.pagetop {
  border: 1px solid #ccc;
  display: -webkit-box;
}
.search-title {
  margin: 0 5px 0 20px;
  font-size: 12px;
}
.top-deal {
  right: 5px;
  top: 5px;
}
.pagecenter {
  margin-top: 5px;
  overflow: auto;
}

.middle-center {
  left: 5px;
  right: 5px;
  overflow: auto;
}

.table1style tr td {
  padding: 0px 5px 0px 5px;
}

.table2style tr td {
  padding: 3px 2px 3px 5px;
}
::v-deep .RechkBack {
  background-color: #c0ffc0;
}
</style>
